package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.OrderSetting;
import com.itheima.message.MessageConstant;
import com.itheima.result.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    //添加预约设置
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //将excel转换为list集合，集合中的元素为excel中每一行元素的数组
            List<String[]> list =POIUtils.readExcel(excelFile);
            if (list!=null&&list.size()>0){
                //创建OrderSetting的list集合
                List<OrderSetting> orderSettingList=new ArrayList<>();
                //遍历数组，拿到每个元素，将元素赋值给对象，再存入集合中
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                //调用Service方法将集合传过去,返回一个布尔值
                boolean flag = orderSettingService.add(orderSettingList);
                if (!flag){
                    //布尔值为false，则返回
                    return new Result(false,"预约设置人数小于已预约人数，请检查完重新添加");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
    //查看预约设置
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            //调用Service方法，返回预约设置的map集合
            List<Map> list=orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    //修改设置
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            //调用Service方法，返回一个布尔值
            boolean flag = orderSettingService.editNumberByDate(orderSetting);
            //判断
            if (!flag){
                //布尔值为false，则返回
                return new Result(false,"预约设置人数小于已预约人数，请重新设置");
            }
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
